package com.horoscope.astrology.zodiac.signs.free.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.activity.PreMainActivity
import com.horoscope.astrology.zodiac.signs.free.utils.ConstellationUtil
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import kotlinx.android.synthetic.main.fragment_relationship.*

/**
 * 目前的状态（单身、已婚、寻找灵魂伴侣、黄金圣战士）
 * */
class RelationshipFragment : BaseFragment(), View.OnClickListener {
    private var single: RadioButton? = null
    private var soulmate: RadioButton? = null
    private var married: RadioButton? = null
    private var difficult: RadioButton? = null
    private var mReply: TextView? = null
    private var mReplyLinear: LinearLayout? = null
    private var mStar: ImageView? = null
    private var once = true

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_relationship, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        inVisibilityRoot()
    }

    private fun initView(view: View) {
        mStar = view.findViewById(R.id.image_view_star)
        single = view.findViewById(R.id.radio_button_single)
        soulmate = view.findViewById(R.id.radio_button_soul)
        married = view.findViewById(R.id.radio_button_married)
        difficult = view.findViewById(R.id.radio_button_difficult)
        mReply = view.findViewById(R.id.text_view_reply_relationship)
        mReplyLinear = view.findViewById(R.id.linear_layout_reply_relationship)
        mRoot = view.findViewById(R.id.linear_root_relationship)
        single?.setOnClickListener(this)
        soulmate?.setOnClickListener(this)
        married?.setOnClickListener(this)
        difficult?.setOnClickListener(this)
    }


    fun setConstellationName(name: String) {
        mStar!!.setImageResource(ConstellationUtil.getIconByName(name))

    }

    private var mName = ""
    @SuppressLint("SetTextI18n")
    fun setName(name: String) {
        mName = name
        setGuideContent(text_view_content_place, mName, getString(R.string.tips_relationship))
        //text_view_content_place?.text = name + "! " + getString(R.string.tips_relationship)
    }

    private fun setGuideContent(textView: TextView, name: String, str: String) {
        if (!SysUtils.isContainChinese(mName)) {
            textView.text = "$mName! $str"
        } else {
            val byteSize = name.toByteArray().size
            val newByte = ByteArray(18)
            if (byteSize > 18) {
                for (i in 0..17) {
                    newByte[i] = name.toByteArray()[i]
                }
                textView.text = String(newByte) + "...! " + str
            } else {
                textView.text = "$name! $str"
            }
        }
    }

    private fun relationshipAnimListener(
        tag: Int,
        anim: Animation,
        mReply: TextView,
        replay: String
    ) {
        anim.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                when (tag) {
                    1 -> {
                        single?.clearAnimation()
                        single?.visibility = View.INVISIBLE
                    }
                    2 -> {
                        soulmate?.clearAnimation()
                        soulmate?.visibility = View.INVISIBLE

                    }
                    3 -> {
                        married?.clearAnimation()
                        married?.visibility = View.INVISIBLE
                    }
                    4 -> {
                        difficult?.clearAnimation()
                        difficult?.visibility = View.INVISIBLE
                    }
                }

                mReply.text = replay
                showReply()
            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })
    }

    private fun showReply() {
        val fadeIn = AnimationUtils.loadAnimation(activity, R.anim.fade_in)
        mReplyLinear?.startAnimation(fadeIn)
        fadeIn.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                mReplyLinear?.postDelayed({
                    fadeOutRoot()
                }, 200)

            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })
    }

    override fun onClick(v: View?) {
        if (!once) {
            return
        }
        var relationship = ""
        when (v?.id) {
            R.id.radio_button_single -> {
                val anim = AnimationUtils.loadAnimation(activity, R.anim.single)
                single?.startAnimation(anim)
                difficult?.visibility = View.INVISIBLE
                married?.visibility = View.INVISIBLE
                soulmate?.visibility = View.INVISIBLE

                val drawable = ContextCompat.getDrawable(activity, R.drawable.ic_single_color)
                single?.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null)
                //mReply?.text = getString(R.string.label_single)
                relationship = getString(R.string.label_single)
                relationshipAnimListener(1, anim, mReply!!, relationship)
            }
            R.id.radio_button_soul -> {
                val anim = AnimationUtils.loadAnimation(activity, R.anim.soulmate)
                soulmate?.startAnimation(anim)
                difficult?.visibility = View.INVISIBLE
                married?.visibility = View.INVISIBLE
                single?.visibility = View.INVISIBLE

                val drawable = ContextCompat.getDrawable(activity, R.drawable.ic_soulmate_color)
                soulmate?.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null)
                //mReply?.text = getString(R.string.label_soulmate)
                relationship = getString(R.string.label_soulmate)
                relationshipAnimListener(2, anim, mReply!!, relationship)
            }
            R.id.radio_button_married -> {
                val anim = AnimationUtils.loadAnimation(activity, R.anim.married)
                married?.startAnimation(anim)
                difficult?.visibility = View.INVISIBLE
                single?.visibility = View.INVISIBLE
                soulmate?.visibility = View.INVISIBLE

                val drawable = ContextCompat.getDrawable(activity, R.drawable.ic_married_color)
                married?.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null)
                //mReply?.text = getString(R.string.label_married)
                relationship = getString(R.string.label_married)
                relationshipAnimListener(3, anim, mReply!!, relationship)
            }
            R.id.radio_button_difficult -> {
                val anim = AnimationUtils.loadAnimation(activity, R.anim.difficult)
                difficult?.startAnimation(anim)
                single?.visibility = View.INVISIBLE
                married?.visibility = View.INVISIBLE
                soulmate?.visibility = View.INVISIBLE

                val drawable = ContextCompat.getDrawable(activity, R.drawable.ic_difficult_color)
                difficult?.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null)
                //mReply?.text = getString(R.string.label_difficult)
                relationship = getString(R.string.label_difficult)
                relationshipAnimListener(4, anim, mReply!!, relationship)
            }
        }
        (activity as PreMainActivity).setMarial(relationship)
        once = false
    }

    fun fadeOutRoot() {
        val anim = AnimationUtils.loadAnimation(activity, R.anim.fade_out)
        mRoot?.startAnimation(anim)
        anim.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                if (activity is PreMainActivity) {
                    (activity as PreMainActivity).setPage(5)
                }
            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })
    }

    fun fadeIn() {
        super.fadeInRoot(mRoot!!)
    }
}